// pages/components/my-swiper.js
const {getFilterTypes} = require('../../utils/netUtils');

Component({
    options: {
        addGlobalClass: true,
    },
    /**
     * 组件的属性列表
     */
    properties: {
        // 这里定义了属性，属性值可以在组件使用时指定
        type: {
            type: String,
            value: '',
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        sortArr: ['全部', '新展来了', '精彩推荐', '大家想去'],
        stateArr: ['全部', '预告', '展中', '结束'],
        timeArr: ['全部', '一月内', '一周内', '本周末'],
        filterArr: [],
        filterResultArr: [],
        stateShow: false,
        sortShow: false,
        timeShow: false,
        filterShow: false,
        stateSelect: 0,
        timeSelect: 0,
        sortSelect: 0,
        filterResultTxt:''
    },

    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () {
            console.log('component filter-bar attached');
            this.animation = wx.createAnimation({duration:600,timingFunction:'ease-in-out'});
            getFilterTypes(ret=>{
                console.log(ret);
                if(ret.status==='200'){
                    let result = [];
                    for(let i in ret.data){
                        result.push({txt:ret.data[i],check:false});
                    }
                    if(result.length>0){
                        this.setData({filterArr:result});
                    }
                }
            });
        },
        moved: function () {
        },
        detached: function () {
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        setType:function(type){
            let initData={stateSelect: 0,sortSelect: 0,};
            if(type==='new'){
                //title = '新展来了';
                initData.sortSelect=1;
            }else if(type==='recommend'){
                //title = '编辑推荐';
                initData.sortSelect=2;
            }else if(type==='want'){
                //title = '大家想去';
                initData.sortSelect=3;
            }

            this.setData(initData);
        },
        onMenuItemTap: function (e) {
            //console.log(e)
            let idx = parseInt(e.currentTarget.dataset.idx);
            let type = e.currentTarget.dataset.type;
            if (type === 'time') {
                let timeSelectText = this.data.timeArr[idx];
                if (timeSelectText === '全部') {
                    timeSelectText = '';
                }
                this.setData({timeSelect: idx, filterShow: false, stateShow: false, timeShow: false, sortShow: false,timeSelectText},this.doNotice);
            } else if (type === 'state') {
                let stateSelectText = this.data.stateArr[idx];
                if (stateSelectText === '全部') {
                    stateSelectText = '';
                }
                this.setData({stateSelect: idx, filterShow: false, stateShow: false, timeShow: false,sortShow: false, stateSelectText},this.doNotice);
            } else if(type === 'sort'){
                let sortSelectText = this.data.sortArr[idx];
                if (sortSelectText === '默认') {
                    sortSelectText = '';
                }
                this.setData({sortSelect: idx, filterShow: false, stateShow: false, timeShow: false,sortShow: false, sortSelectText},this.doNotice);
            }
        },

        doNotice(){
            this.triggerEvent('sendResult', {
                state: {
                    idx: this.data.stateSelect,
                    content: this.data.stateSelectText
                },
                time:{
                    idx:this.data.timeSelect,
                    content:this.data.timeSelectText
                },
                sortt:{
                    idx:this.data.sortSelect,
                    content:this.data.sortSelectText
                },
                filter:this.data.filterResultArr
            }, {});
        },

        onFilterCheck: function (e) {//筛选条件可多选
            let idx = e.currentTarget.dataset.idx;
            let contentObj = this.data.filterArr[idx];
            let resultArr = this.data.filterResultArr;
            let tempIndex = resultArr.indexOf(contentObj.txt);
            if (tempIndex>=0){
                resultArr.splice(tempIndex,1);
                contentObj.state=false;
            } else {
                resultArr.push(contentObj.txt);
                contentObj.state=true;
            }
            this.data.filterArr[idx] = contentObj;
            this.setData({filterResultArr: resultArr,filterResultTxt:resultArr.join(','),filterArr:this.data.filterArr});
        },
        resetFilter: function () {
            let filterArr = this.data.filterArr;
            for (let i = 0; i < filterArr.length; i++) {
                filterArr[i].state = false;
            }
            this.setData({filterResultArr: [],filterResultTxt:'',filterArr});
        },
        confirmFilter: function () {
            this.onWrapperTap();
            this.triggerEvent('sendResult', {
                state: {
                    idx: this.data.stateSelect,
                    content: this.data.stateSelectText
                },
                time:{
                    idx:this.data.timeSelect,
                    content:this.data.timeSelectText
                },
                sortt:{
                    idx:this.data.sortSelect,
                    content:this.data.sortSelectText
                },
                filter:this.data.filterResultArr
            }, {});
        },
        toggleState: function () {
            let result = !this.data.stateShow;
            if(result){
                this.setData({stateShow: true, timeShow: false, filterShow: false,sortShow: false});
            }else{
                this.hideStatePanel();
            }
        },
        toggleTime: function () {
            let result = !this.data.timeShow;
            if(result){
                this.setData({timeShow: result, stateShow: false, filterShow: false,sortShow: false});
            }else{
                this.hideTimePanel();
            }
        },
        toggleFilter: function () {
            let result = !this.data.filterShow;
            if(result){
                this.setData({filterShow: true, stateShow: false, timeShow: false,sortShow: false});
            }else{
                this.hideFilterPanel();
            }
        },
        toggleSort: function () {
            let result = !this.data.sortShow;
            if(result){
                this.setData({sortShow: true, stateShow: false, timeShow: false,filterShow: false});
            }else{
                this.hideSortPanel();
            }
        },
        onWrapperTap: function () {
            if(this.data.stateShow){
                this.hideStatePanel();
            }else if(this.data.timeShow){
                this.hideTimePanel();
            }else if(this.data.sortShow){
                this.hideSortPanel();
            }else{
                this.hideFilterPanel();
            }
        },
        blankFun: function () {
        },


        hideStatePanel(){
            this.animation.translate(0, -200).step();
            this.setData({stateAnimation: this.animation.export()},()=>{
                setTimeout(()=>{
                    this.setData({stateShow: false,stateAnimation:{}});
                },100);
            })
        },
        hideTimePanel(){
            this.animation.translate(0, -200).step();
            this.setData({timeAnimation: this.animation.export()},()=>{
                setTimeout(()=>{
                    this.setData({timeShow: false,timeAnimation:{}});
                },100);
            })
        },
        hideFilterPanel(){
            this.animation.translate(700,0).step();
            this.setData({filterAnimation: this.animation.export()},()=>{
                setTimeout(()=>{
                    this.setData({filterShow: false,filterAnimation:{}});
                },100);
            })
        },
        hideSortPanel(){
            this.animation.translate(0, -200).step();
            this.setData({sortAnimation: this.animation.export()},()=>{
                setTimeout(()=>{
                    this.setData({sortShow: false,sortAnimation:{}});
                },100);
            })
        },
    }
});
